<template>
  <div>
    <el-table :data="borrowInfos" style="width: 100%">
      <el-table-column label="序号" type="index"></el-table-column>
      <el-table-column prop="name" label="姓名" width="80"></el-table-column>
      <el-table-column prop="mobile" label="手机" width="120"></el-table-column>
      <el-table-column prop="amount" label="借款金额"></el-table-column>
      <el-table-column prop="period" label="借款期限">
        <template slot-scope="scope">
          <i>{{ scope.row.period + '个月' }}</i>
        </template>
      </el-table-column>
      <el-table-column prop="returnMethod" label="还款方式"></el-table-column>
      <el-table-column prop="moneyUse" label="资金用途"></el-table-column>
      <el-table-column prop="borrowYearRate" label="年化利率">
        <template slot-scope="scope">
          <i>{{ scope.row.borrowYearRate + '%' }}</i>
        </template>
      </el-table-column>
      <el-table-column prop="status" label="状态"></el-table-column>
      <el-table-column
        prop="createTime"
        label="申请时间"
        width="180"
      ></el-table-column>
      <el-table-column label="操作" width="180">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="primary"
            @click="handleShow(scope.$index, scope.row)"
          >
            查看
          </el-button>
          <el-button
            size="mini"
            type="warning"
            @click="handleApproval(scope.$index, scope.row)"
          >
            审批
          </el-button>
        </template>
      </el-table-column>
    </el-table>
    <div style="text-align: center">
      <el-pagination
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
        :current-page="pageNum"
        :page-sizes="[1, 3, 5, 10]"
        :page-size="pageSize"
        layout="total, sizes, prev, pager, next, jumper"
        :total="total"
      ></el-pagination>
    </div>

    <!-- 审批对话框 -->
    <el-dialog title="审批" :visible.sync="dialogVisible" width="490px">
      <el-form label-position="right" label-width="100px">
        <el-form-item label="是否通过">
          <el-radio-group v-model="borrowInfoApproval.status">
            <el-radio :label="2">通过</el-radio>
            <el-radio :label="-1">不通过</el-radio>
          </el-radio-group>
        </el-form-item>

        <el-form-item v-if="borrowInfoApproval.status == 2" label="标的名称">
          <el-input v-model="borrowInfoApproval.title" />
        </el-form-item>

        <el-form-item v-if="borrowInfoApproval.status == 2" label="起息日">
          <el-date-picker
            v-model="borrowInfoApproval.lendStartDate"
            type="date"
            placeholder="选择开始时间"
            value-format="yyyy-MM-dd"
          />
        </el-form-item>

        <el-form-item v-if="borrowInfoApproval.status == 2" label="年化收益率">
          <el-input v-model="borrowInfoApproval.lendYearRate">
            <template slot="append">%</template>
          </el-input>
        </el-form-item>

        <el-form-item v-if="borrowInfoApproval.status == 2" label="服务费率">
          <el-input v-model="borrowInfoApproval.serviceRate">
            <template slot="append">%</template>
          </el-input>
        </el-form-item>

        <el-form-item v-if="borrowInfoApproval.status == 2" label="标的描述">
          <el-input v-model="borrowInfoApproval.lendInfo" type="textarea" />
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="dialogVisible = false">取消</el-button>
        <el-button type="primary" @click="approvalSubmit">确定</el-button>
      </div>
    </el-dialog>
  </div>
</template>
<script>
import borrowerApi from '@/api/borrower'
export default {
  data() {
    return {
      borrowInfoApproval: {
        status: 2,
        serviceRate: 5,
      },
      dialogVisible: false,
      borrowInfos: [],
      pageNum: 1,
      pageSize: 3,
      total: 0,
      key: '',
    }
  },
  created() {
    this.getBorrowInfos()
  },
  methods: {
    //审批对话框提交按钮单击事件
    approvalSubmit() {
      //提交审批请求
      borrowerApi.borrowInfoApproval(this.borrowInfoApproval).then((r) => {
        this.$message.success(r.message)
        this.dialogVisible = false //关闭对话框
        //刷新页面 显示最新的审核状态
        this.getBorrowInfos()
      })
    },
    //弹出审批的对话框
    handleApproval(index, row) {
      this.dialogVisible = true
      console.log(row)
      this.borrowInfoApproval.lendYearRate = row.borrowYearRate
      this.borrowInfoApproval.id = row.id
    },
    handleShow(index, row) {
      //查看 按钮单击事件
      // console.log(row)
      this.$router.push(`/borrower/detail/${row.id}`)
    },
    handleSizeChange(size) {
      this.pageSize = size
      this.getBorrowInfos()
    },
    handleCurrentChange(num) {
      this.pageNum = num
      this.getBorrowInfos()
    },
    getBorrowInfos() {
      borrowerApi
        .getBorrowInfos(this.pageNum, this.pageSize, this.key)
        .then((r) => {
          this.borrowInfos = r.data.page.records
          this.total = r.data.page.total
        })
    },
  },
}
</script>
